package com.gsmr.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.gsmr.service.InitDatabase;
import com.gsmr.util.PropertityUtil;

/**
 * 初始化组件 
 *
 */
@Component("initComponent")
public class InitComponent implements ServletContextListener,ApplicationContextAware{
/*
 * 初始化准备工作需要先在web.xml中配置listener
 */
	private static ApplicationContext applicationContext;
	
	public void refreshSystem(ServletContext application){
		InitDatabase initDatabase=(InitDatabase) applicationContext.getBean("InitDatabase");
		Map map = new HashMap();
		map.put("database_name", PropertityUtil.GSMR_DATABASE);
		
		initDatabase.buildDatabase(map);
		initDatabase.buildTables(map);
		//new MainController().gsmrMain();
	}
	
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext application=servletContextEvent.getServletContext();
		refreshSystem(application);
	}

	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

	
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}
}
